package org.middleware.controller.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.middleware.enums.HttpResponseCode;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonHttpResponse<T> {
    private Integer code;
    private String msg;
    private T data;
    private Long timestamp;
    private String path;

    public CommonHttpResponse() {
        this.timestamp = System.currentTimeMillis();
    }

    public CommonHttpResponse(Integer code, String msg, T data) {
        this();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功响应，返回默认消息
     * @return HttpResponse
     * @param <T>
     */
    public static <T> CommonHttpResponse<T> success() {
        return new CommonHttpResponse<>(HttpResponseCode.SUCCESS.getCode(), HttpResponseCode.SUCCESS.getMsg(), null);
    }

    /**
     * 成功响应，自定义返回消息
     * @param msg 消息内容
     * @return HttpResponse
     * @param <T>
     */
    public static <T> CommonHttpResponse<T> success(String msg) {
        return new CommonHttpResponse<>(HttpResponseCode.SUCCESS.getCode(), msg, null);
    }

    /**
     * 成功响应，返回data
     * @param data json数据
     * @return
     * @param <T>
     */
    public static <T> CommonHttpResponse<T> success(T data) {
        return new CommonHttpResponse<>(HttpResponseCode.SUCCESS.getCode(), HttpResponseCode.SUCCESS.getMsg(), data);
    }

    public static <T> CommonHttpResponse<T> success(String msg, T data) {
        return new CommonHttpResponse<>(HttpResponseCode.SUCCESS.getCode(), msg, data);
    }

    // 失败响应
//    public static <T> CommonHttrResponse<T> fail() {
//        return new CommonHttrResponse<>(HttpResponseCode.FAILED.getCode(), HttpResponseCode.FAILED.getMsg(), null);
//    }
//
//    public static <T> CommonHttrResponse<T> fail(String msg) {
//        return new CommonHttrResponse<>(HttpResponseCode.FAILED.getCode(), msg, null);
//    }

    public static <T> CommonHttpResponse<T> fail(Integer code, String msg) {
        return new CommonHttpResponse<>(code, msg, null);
    }

    public static <T> CommonHttpResponse<T> fail(HttpResponseCode HttpResponseCode) {
        return new CommonHttpResponse<>(HttpResponseCode.getCode(), HttpResponseCode.getMsg(), null);
    }

    public static <T> CommonHttpResponse<T> fail(HttpResponseCode HttpResponseCode, String msg) {
        return new CommonHttpResponse<>(HttpResponseCode.getCode(), msg, null);
    }

    // 链式调用方法
    public CommonHttpResponse<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public CommonHttpResponse<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public CommonHttpResponse<T> data(T data) {
        this.data = data;
        return this;
    }

    public CommonHttpResponse<T> path(String path) {
        this.path = path;
        return this;
    }
}